//
//  SEGAwardManager.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 16/11/25.
//  Copyright © 2016年 陈区. All rights reserved.
//

#import "SEGAwardManager.h"

#if __has_include(<Commom/SEGBasicBussinessCommonDefine.h>)
#import <Commom/SEGBasicBussinessCommonDefine.h>
#else
#import "SEGBasicBussinessCommonDefine.h"
#endif

#pragma mark - 抽奖API
#define ACTION_CHECK_ACTIVITY [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_app,@"/ecomm/activity/checkActivity.json",nil]
//是否可以抽奖结果Draw Results
#define DRAW_RESULTS [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_app,@"/ecomm/activity/startActByDoor.json",nil]
//中奖结果
#define AWARDS_RESULTS [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_app,@"/ecomm/activity/activityUnpackWin.json",nil]

@interface SEGAwardManager ()
/**
 *  抽奖类型参数 ：（开门抽奖：opendoor，自助洗车评论抽奖：flashbox）
 */
@property (nonatomic, copy) NSString* from;

@end

static id _awardManager;
static dispatch_once_t onceToken;

@implementation SEGAwardManager

+(instancetype)sharedManager{
    dispatch_once(&onceToken, ^{
        _awardManager = [[self alloc] init];
    });
    return _awardManager;
}

+(void)destroyResource{
    onceToken = 0;
    _awardManager = nil;
}

- (instancetype)initWithAwardType:(SEGAwardFromType)type
{
    self = [super init];
    if (self) {
        switch (type) {
            case SEGAwardFromType_OpenDoor:
            {
                _from = @"opendoor";
                break;
            }
            case SEGAwardFromType_FlashBox:
            {
                _from = @"flashbox";
                break;
            }
        }
    }
    return self;
}
/**
 *  检测是否有抽奖
 *  @param finished 网络请求完成后的操作
 */
- (void)requestCheckActivity:(SEGNetworkRequestCallBack)finished
{
    NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:@"opendoor", @"from", nil];
    
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:ACTION_CHECK_ACTIVITY parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        finished(result, nil);
    }];
}

/**
 *  H5抽奖判断
 *  @param finished 网络请求完成后的操作
 */
- (void)requestCheckActivityAfterOpenDoor:(SEGAwardManagerCallBack)finished
{
    NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:self.from, @"from", nil];

    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:ACTION_CHECK_ACTIVITY parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        if (!isSuccessed) {
            finished(isSuccessed, kNetworkErrorMessage, 0, nil);//数据返回异常
        }
        
        NSDictionary* dicData = [result valueForKey:@"data"];
        if ((!dicData) || (![dicData isKindOfClass:[NSDictionary class]])) {
            finished(NO, errorMessage, 0, nil);//数据返回异常
            return;
        }
        
        if ([dicData valueForKey:@"forwardUrl"]) {
            SEGAwardForwardType forwardType = [[dicData valueForKey:@"forwardType"] intValue];
            switch (forwardType) {
                case SEGAwardForwardType_Web://网页形式 直接跳转
                {
                    finished(YES, errorMessage, SEGAwardForwardType_Web, dicData);
                    break;
                }
                default:
                {
                    [self requestStartActByDoor:^(id result, NSError* error) {
                        //                            [self dealWithStartActByDoorResultDict:result forwardType:forwardType];
                        if ([codeType isEqualToString:@"0"]) {
                            NSDictionary* data = [result objectForKey:@"data"];
                            if (data && [data isKindOfClass:[NSDictionary class]]) {
                                switch (forwardType) {
                                    case SEGAwardForwardType_RedWallet: { //普通红包
                                        SEGAwardPrizeType prizeType = [[data objectForKey:@"prizeType"] intValue];
                                        switch (prizeType) {
                                            case kPrizeTypeProduct:
                                            case kPrizeTypeRedWallet:
                                            case kPrizeTypeCard:
                                            {
                                                //红包卡卷(点击才请求中奖结果)
                                                finished(YES, nil, SEGAwardForwardType_RedWallet, nil);
                                                //                        [self.delegate showRedWallet];
                                                break;
                                            }
                                            case kPrizeTypeGroupShopping:
                                            case kPrizeTypeHomeService:
                                            case kPrizeTypePictorial:
                                            {
                                                //新增内容广告弹窗 优团购:40 服务到家:50 画报:60
                                                [self requestAwardsResult:^(id result, NSError* error) {
                                                    if ([codeType isEqualToString:@"0"]) {
                                                        finished(YES, nil, SEGAwardForwardType_AD, [result objectForKey:@"data"]);
                                                    }
                                                }];
                                                break;
                                            }
                                            default:
                                                break;
                                        }
                                    } break;
                                    case SEGAwardForwardType_Scratch: { //刮奖
                                        [self requestAwardsResult:^(id result, NSError* error) {
                                            finished(YES, nil, SEGAwardForwardType_Scratch, result);
                                        }];
                                    } break;
                                    default:
                                        break;
                                }
                            }
                        }
                    }];
                    break;
                }
            }
        }
    }];
}

/**
 *  是否可以抽奖结果Draw Results
 *  @param finished 网络请求完成后的操作
 */
- (void)requestStartActByDoor:(SEGNetworkRequestCallBack)finished
{
    NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:self.from, @"from", nil];

    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:DRAW_RESULTS parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        finished(result, nil);
    }];
}

/**
 *  中奖结果
 *  @param finished 网络请求完成后的操作
 */
- (void)requestAwardsResult:(SEGNetworkRequestCallBack)finished
{
    NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:self.from, @"from", nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:AWARDS_RESULTS parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        finished(result, nil);
    }];
}

#pragma 处理请求结果
/**
 *  处理中奖结果
 *
 *  @param dict        result
 *  @param forwardType 网页形式:10, 普通红包:20, 红包雨:30, 刮奖:40, 翻牌:50
 */
- (void)dealWithStartActByDoorResultDict:(NSDictionary*)dict forwardType:(SEGAwardForwardType)forwardType
{

}

/**
 *  抽奖参数form 默认opendoor
 *
 *  @return 抽奖参数form
 */
- (NSString*)from
{
    if (!_from) {
        _from = @"opendoor";
    }
    return _from;
}
@end
